<?php
# ***** BEGIN LICENSE BLOCK *****
# This file is part of "myWiWall".
# Copyright (c) 2007-2008 CRP Henri Tudor and contributors.
# All rights reserved.
#
# "myWiWall" is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# 
# "myWiWall" is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with "myWiWall"; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# ***** END LICENSE BLOCK *****


class Rijndael
{
	private $iv;
	
	public function __construct()
	{
		$this->iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
	}
	
	public function generateKey()
	{
		$keyString = md5(uniqid (rand()));
		$secureKey = hash('sha256', $keyString, true);
		
		return $secureKey;
	}
	
	public function encrypt($in, $key)
	{
		return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $in, MCRYPT_MODE_ECB, $this->iv);
	}
	
	public function decrypt($in, $key)
	{
		// the mcrypt_decrypt function seems to always 'tag' the end of the decrypted data by \0. It certainly comes from it's 'C' nature. So we replace the \0 by ''.
		return str_replace("\x0", '', mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $in, MCRYPT_MODE_ECB, $this->iv));
	}
}
?>